/*
 * Copyright 2021 tu.cn All right reserved. This software is the
 * confidential and proprietary information of tu.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Tu.cn
 */
package com.genersoft.iot.vmp.vmanager.record;

import com.genersoft.iot.vmp.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author rox_p
 * @date 2021/9/26 15:20
 */

public class TcpTrans extends Thread {
    int appPort;
    int ipcPort;
    String deviceId;
    RedisUtil redisUtil;
    public TcpTrans(int appPort, int ipcPort,String deviceId,RedisUtil redisUtil) {
        this.appPort = appPort;
        this.ipcPort = ipcPort;
        this.deviceId = deviceId;
        this.redisUtil = redisUtil;
    }

    /*public void run() {
        try {
            ServerSocket appSocket = new ServerSocket(this.appPort);
            ServerSocket ipcSocket = new ServerSocket(this.ipcPort);
            Socket appClientSocket = null;
            Socket ipcClientSocket = null;
            while (true) {
                try {
                    appClientSocket = appSocket.accept();
                    System.out.println("端口号"+appClientSocket.getPort());
                    if(ipcClientSocket == null)
                        ipcClientSocket = ipcSocket.accept();

                    (new TransPortData(appClientSocket, ipcClientSocket)).start();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }*/

    public void run() {
        try {
            ServerSocket appSocket = new ServerSocket(this.appPort);
            ServerSocket ipcSocket = new ServerSocket(this.ipcPort);
            Socket appClientSocket = null;
            Socket ipcClientSocket = null;
            boolean threadStop = true;
            while (threadStop) {
                try {
                    appClientSocket = appSocket.accept();
                    System.out.println("端口号"+appClientSocket.getPort());
                    if(ipcClientSocket == null)
                        ipcClientSocket = ipcSocket.accept();

                    boolean isRunning = true;
                    InputStream in = null;
                    OutputStream out = null;
                    try {
                        in = appClientSocket.getInputStream();
                        out = ipcClientSocket.getOutputStream();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    while (isRunning) {

                        try {
                            // 读入数据
                            byte[] data = new byte[400];

                            int readlen = in.read(data);
                            System.out.println("发送消息长度" + readlen);

                            // 如果没有数据，则暂停
                            if (readlen <= 0) {
                                Thread.sleep(300);
                                isRunning = false;
                            }

                            RtspPacketEncode rtspPacketEncode = new RtspPacketEncode();
                            rtspPacketEncode.packet_g711a(data, data.length, out);
//                out.flush();
                        } catch (Exception e) {
                            isRunning = false;
                            threadStop = false;
                            e.printStackTrace();
                        }
                    }
//                    try {
//                        System.out.println("關閉socket");
//                        if (out != null) {
//                            out.close();
//                        }
//                        if (in != null) {
//                            in.close();
//                        }
//                        if (appClientSocket != null) {
//                            appClientSocket.close();
//                        }
//                        if (ipcClientSocket != null) {
//                            ipcClientSocket.close();
//                        }
//                    } catch (Exception ex) {
//                        ex.printStackTrace();
//                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    threadStop = false;
                }
            }
            System.out.println("關閉socket");
            if (appClientSocket != null) {
                appClientSocket.close();
            }
            if (ipcClientSocket != null) {
                ipcClientSocket.close();
            }
            if(appSocket!=null){
                appSocket.close();
            }
            if(ipcSocket!=null){
                ipcSocket.close();
            }
            //清除緩存
            redisUtil.del("APPPORT_"+deviceId);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
}